package com.dliu.javadesign.behaviour.command;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yanglei
 * date 2021/4/23 12:03
 */
public class TVWatcher {
    List<TVCommand> history = new ArrayList<>();

    public void change(TVCommand tvCommand){
        history.add(tvCommand);
        tvCommand.execute();
    }

    public void back(){
        if (history.isEmpty()){
            System.out.println("没有记录");
            return;
        }

        int size = history.size();
        int pre = Math.max(size - 2, 0);
        TVCommand command = history.remove(pre);
        command.execute();
    }

    public static void main(String[] args) {
        TVWatcher tvWatcher = new TVWatcher();
        tvWatcher.change(new CCTV1Command());
        tvWatcher.change(new CCTV1Command());
        tvWatcher.change(new CCTV2Command());
        tvWatcher.change(new CCTV3Command());
        tvWatcher.change(new CCTV5Command());
        tvWatcher.back();
        tvWatcher.back();
    }
}
